"""
按周期计算是否新高新低，简单方法
"""
import pandas as pd
class isnhnl:
    def __init__(self,timeperiod=10):
        self.timeperiod=timeperiod
        self.data=[]
        self.timekey = None
    def oncalc(self,d,timekey=None):
        rst=None
        if not pd.isna(d):
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)==self.timeperiod:
                if max(self.data)==d:
                    rst=1
                elif min(self.data)==d:
                    rst=-1
                else:
                    rst=0
        return rst
class isnhnl_indays:
    def __init__(self,timeperiod=10,indays=5,fx="newhigh"):
        self.timeperiod=timeperiod
        self.indays=indays
        self.data=[]
        self.timekey = None
        self.rsts=[]
        if fx=="newhigh":
            self.oncalc=self.oncalc_newhigh
        else:
            self.oncalc = self.oncalc_newlow
    def oncalc_newhigh(self,d,timekey=None):
        rst=None
        if not pd.isna(d):
            isupdate=False
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                    isupdate=True
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)==self.timeperiod:
                if max(self.data)==d:
                    rst=1
                else:
                    rst=0
                if self.indays>1:
                    if isupdate:
                        self.rsts[-1]=rst
                    else:
                        self.rsts.append(rst)
                    if len(self.rsts)>self.indays:
                        self.rsts.pop(0)
                    if sum(self.rsts)>=1:
                        return 1
        return rst
    def oncalc_newlow(self,d,timekey=None):
        rst=None
        if not pd.isna(d):
            isupdate=False
            if timekey   is not None:
                if timekey == self.timekey:
                    self.data[-1] = d
                    isupdate=True
                else:
                    self.data.append(d)
                    self.timekey = timekey
            else:
                self.data.append(d)
            if len(self.data)>self.timeperiod:
                self.data.pop(0)
            if len(self.data)==self.timeperiod:
                if min(self.data)==d:
                    rst=1
                else:
                    rst=0
                if self.indays>1:
                    if isupdate:
                        self.rsts[-1]=rst
                    else:
                        self.rsts.append(rst)
                    if len(self.rsts)>self.indays:
                        self.rsts.pop(0)
                    if sum(self.rsts)>1:
                        return 1
        return rst